package co.fitstart.mobile.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.utils.StringUtils;
import com.demo2do.core.wechat.web.interceptor.WechatJsConfig;

import co.fitstart.entity.coupon.Coupon;
import co.fitstart.entity.coupon.CouponBatch;
import co.fitstart.entity.user.User;
import co.fitstart.mobile.service.CouponService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;
import co.fitstart.mobile.web.interceptor.WechatAuthority;
import co.fitstart.mobile.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("")
public class WechatCouponClaimController {
    
    private static final Logger logger = LoggerFactory.getLogger(WechatCouponClaimController.class);
    
    @Value("#{config['didi.coupon.ids']}")
    private String didiCouponIdsString;
    
    @Value("#{config['qrcode.coupon.ids']}")
    private String qrcodeCouponIdsString;
    
    @Value("#{config['year.coupon.ids']}")
    private String yearCouponIdsString;
    
    @Value("#{config['bmi.coupon.ids']}")
    private String bmiCouponIdsString;
    
    @Value("#{config['wheely.coupon.ids']}")
    private String wheelyCouponIdsString;
    
    @Autowired
    private CouponService couponService;
    
    /**
     * 
     * @param user
     * @param couponIdsString
     * @return
     */
    protected List<Coupon> drawCoupons(User user, String couponIdsString) {
        
        String[] couponIdsArray = StringUtils.split(couponIdsString, ",");
        
        List<Long> batchhIds = new ArrayList<Long>(couponIdsArray.length);
        for(String couponIdString : couponIdsArray) {
            batchhIds.add(Long.parseLong(couponIdString));
        }
        
        List<CouponBatch> batches = couponService.listCouponBatch(batchhIds);
        couponService.createCoupons(user, batches);
        
        List<Coupon> coupons = couponService.listCoupon(user, batchhIds);
        
        return coupons;
    }
    
    @RequestMapping("/didi-coupons")
    @Router
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView drawDidiCoupon(@Secure User user) {
        
        ModelAndView modelAndView = new ModelAndView("coupon/didi-coupons");
        
        if(user == null) {
            modelAndView.setViewName("auth-error");
            return modelAndView;
        }
        
        try {
            List<Coupon> coupons = this.drawCoupons(user, didiCouponIdsString);
            modelAndView.addObject("coupons", coupons);
            
        } catch (Exception e) {
            
            logger.error("drawDidiCoupon error:", e);
            modelAndView.setViewName("auth-error");
            return modelAndView;
        }
        
        return modelAndView;
    }
    
    /**
     * 
     * @param user
     * @return
     */
    @RequestMapping("/qrcode-coupons")
    @Router
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView drawQrcodeCoupon(@Secure User user) {
        
        ModelAndView modelAndView = new ModelAndView("coupon/qrcode-coupons");
        
        if(user == null) {
            modelAndView.setViewName("auth-error");
            return modelAndView;
        }
        
        try {
           
            List<Coupon> coupons = this.drawCoupons(user, qrcodeCouponIdsString);
            modelAndView.addObject("coupons", coupons);
            
        } catch (Exception e) {
            
            logger.error("drawQrcodeCoupon error:", e);
            modelAndView.setViewName("auth-error");
            return modelAndView;
        }
        
        return modelAndView;
    }
    
    /**
     * 
     * @param user
     * @return
     */
    @RequestMapping("/year-coupons")
    @Router
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView drawYearCoupon(@Secure User user) {
        
        ModelAndView modelAndView = new ModelAndView("coupon/year-coupons");
        
        if(user == null) {
            modelAndView.setViewName("auth-error");
            return modelAndView;
        }
        
        try {
            
            List<Coupon> coupons = this.drawCoupons(user, yearCouponIdsString);
            modelAndView.addObject("coupons", coupons);
            
        } catch (Exception e) {
            
            logger.error("drawCoupon error:", e);
            modelAndView.setViewName("auth-error");
            return modelAndView;
        }
        
        return modelAndView;
    }
    
    /**
     * 
     * @param user
     * @return
     */
    @RequestMapping("/bmi-coupons")
    @Router
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView drawBmiCoupon(@Secure User user) {
        
        ModelAndView modelAndView = new ModelAndView("coupon/bmi-coupons");
        
        if(user == null) {
            logger.error("drawCoupon error: user not exit");
            modelAndView.setViewName("auth-error");
            return modelAndView;
        }
        
        try {
            
            List<Coupon> coupons = this.drawCoupons(user, bmiCouponIdsString);
            modelAndView.addObject("coupons", coupons);
            
        } catch (Exception e) {
            
            logger.error("drawCoupon error:", e);
            modelAndView.setViewName("auth-error");
            return modelAndView;
        }
        
        return modelAndView;
    }
    
    /**
     * 
     * @param user
     * @return
     */
    @RequestMapping("/wheely-coupons")
    @Router
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView drawWheelyCoupon(@Secure User user) {
        
        ModelAndView modelAndView = new ModelAndView("coupon/wheely-coupons");
        
        if(user == null) {
            logger.error("drawCoupon error: user not exit");
            modelAndView.setViewName("auth-error");
            return modelAndView;
        }
        
        try {
            
            List<Coupon> coupons = this.drawCoupons(user, wheelyCouponIdsString);
            modelAndView.addObject("coupons", coupons);
            
        } catch (Exception e) {
            
            logger.error("drawCoupon error:", e);
            modelAndView.setViewName("auth-error");
            return modelAndView;
        }
        
        return modelAndView;
    }
}
